package us.mooresr.webtroopmanager.client.activity;

import java.util.List;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.SecurityManager;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.events.EventAddedEvent;
import us.mooresr.webtroopmanager.client.events.EventAddedHandler;
import us.mooresr.webtroopmanager.client.events.EventDeletedEvent;
import us.mooresr.webtroopmanager.client.events.EventDeletedHandler;
import us.mooresr.webtroopmanager.client.place.EventListPlace;
import us.mooresr.webtroopmanager.client.ui.EventListView;
import us.mooresr.webtroopmanager.shared.EventSummary;
import us.mooresr.webtroopmanager.shared.Privilege;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class EventListActivity extends MyActivity implements EventListView.Presenter {

	
	public EventListActivity(EventListPlace place, ClientFactory clientFactory) {
		super(place, clientFactory);
	}

	
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		
		EventListView eventListView = clientFactory.getEventListView();
		eventListView.setPresenter(this);
		panel.setWidget(eventListView.asWidget());

		populateList(false);
		
		
		eventBus.addHandler(EventAddedEvent.TYPE, new EventAddedHandler() {

			@Override
			public void onAddEvent(EventAddedEvent event) {
				EventListView eventListView = clientFactory.getEventListView();
				eventListView.addEvent(event.getEventSummary());
				
			}
			
		});
		
		eventBus.addHandler(EventDeletedEvent.TYPE,new EventDeletedHandler() {
			
			@Override
			public void onDeleteEvent(EventDeletedEvent event) {
				EventListView eventListView = clientFactory.getEventListView();
				eventListView.removeEvent(event.getDeletedId());
			}
		});
		
	
	}


	@Override
	public void deleteEvent(EventSummary event) {
		Long key = event.getId();
		
		if (Window.confirm("Delete " + event.toString() + "?")) {
			TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
			
			
			AsyncCallback<Long> callback = new AsyncCallback<Long>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(Long result) {
					
					clientFactory.getEventBus().fireEvent(new EventDeletedEvent(result));
				}
			};
			
			troopManagerService.deleteEvent(key, callback);
		}	
		
	}
	
	
	private void populateList(Boolean includePast) {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		
		AsyncCallback<List<EventSummary>> callback = new AsyncCallback<List<EventSummary>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(List<EventSummary> result) {
				
				clientFactory.getEventListView().setData(result);
				clientFactory.getEventListView().setStatus("Ready.");
			}
		};
		clientFactory.getEventListView().setStatus("Retrieving data...");
		if (includePast) {
			troopManagerService.getAllEvents(callback);
		} else {
			troopManagerService.getFutureEvents(callback);
		}
	}


	@Override
	public void showPastEventsChanged(Boolean newValue) {
		// The status of the "Show Past Events" checkbox has changed
		// We need to get the correct list of events and repopulate the view
		
		populateList(newValue);
		
	}

}
